<?php
require_once dirname(__FILE__) . '/../common/Validate.php';


/**
 * Vy för användarhantering
 */
class UsersView {
	private static $k_username = 'users_username';
	private static $k_fullName = 'fullName';
	private static $k_password = 'users_password';
	private static $k_confirmPassword = 'confirmpass';
	private static $k_email = 'email';
	private static $k_register = 'register';
	private static $k_house = 'house';
	private static $k_edit_user = 'edit_user';
	private static $k_delete_user = 'delete_user';
	private static $k_role = 'role';



	public static $houses = array('SPIIK', 'CNaS', 'IDéHuS', 'ESS', 'KAROLIN', 'Meskalin', 'Humanus', 'ESN', 'Lambda');
	public static $roles;

	private $validationErrors = array();
	private $sucessMessages = array();

	public function __construct() {
		$this->roles = array((string) UsersModel::NOT_CONFIRMED_ROLE => 'Ej godkända användare',
							(string) UsersModel::USER_ROLE => 'Godkända användare',
							(string) (UsersModel::USER_ROLE | UsersModel::ADMIN_ROLE) => 'Administratörer');

	}
	
	public function getRegistrationForm(User $user = null, $includeUserRole = false) {		

		if ($user) {
			$title = 'Redigera användaruppgifter';
			$email = $user->email;
			$house = $user->house;
			$username = $user->username;
			$role = $this->roles[$user->role];
			$submit = 'Uppdatera!';
		} else {
			$title = 'Registrera dig';
			$email = '';
			$house = 'Välj förening:';
			$username = '';
			$submit = 'Registrera!';
		}

		$form = '<form method="post">
			<h3>' . $title . '</h3>';

		if ($user) {
			$form .= '<div>
					<a href="http://www.facebook.com/' . $user->username . '"><img src="https://graph.facebook.com/' . $user->username . '/picture" /></a>
					<a href="http://www.facebook.com/' . $user->username . '">' . $user->getFullName() . '</a>
				</div>';
		}
		
		$form .= '<div class="input">
				<label for="' . self::$k_email . '">Epost: </label>
				<input name="' . self::$k_email . '" id="' . self::$k_email . '" value="' . $email . '"/>
			</div>
			<div class="input">
			<label for="' . self::$k_house . '">Förening</label>
				<select name="' . self::$k_house . '" id="' . self::$k_house . '">
				  <option value="' . $house . '">' . $house . '</option>';

		foreach (self::$houses as $h) {
			$form .= '<option value="' . $h . '">' . $h . '</option>';
		}
		$form .= '</select></div>';

		if ($user && $includeUserRole) {
			$form .= '<div class="input">
					<label for="' . self::$k_role . '">Användargrupp</label>
						<select name="' . self::$k_role . '" id="' . self::$k_role . '">
							<option value="' . $role . '">' . $role . '</option>';

			foreach (self::$this->roles as $r) {
				$form .= '<option value="' . $r . '">' . $r . '</option>';
			}

			$form .= '</select></div>';
		}


		$form .= '<input type="hidden" name="' . self::$k_username . '" value="' . $username . '" />
			<input type="submit" name="' . self::$k_register . '" value="' . $submit . '" />
		</form>';
		return $form;
	}

	public function getUsersMarkup($users) {		
		$length = count($users);
		$prevUserRole = 0;
		
		$usersTable = '<div id="members">
			<table>';
		
		for ($i = 0; $i < $length; ++$i) {
			$user = $users[$i];

			if ($prevUserRole != $user->role) {
				$roleTitle = $this->roles[$user->role];
				$usersTable .= '<tr class="user-role-title"><td class="role" colspan="4"><h3>' . $roleTitle . '</h3></td></tr>';
				$prevUserRole = $user->role;

			}
			
			$usersTable .= ($i % 2 === 0) ? '<tr class="odd"><td class="username">' : '<tr class="even"><td class="username">';
			$usersTable .= '<a href="http://www.facebook.com/' . $user->username . '"><img src="https://graph.facebook.com/' . $user->username . '/picture" /></a>';
			$usersTable .= '</td><td class="fullname">';
			$usersTable .= '<a href="http://www.facebook.com/' . $user->username . '">' . $user->getFullName() . '</a>';
			$usersTable .= '</td><td class="email">';
			$usersTable .= $user->email;
			$usersTable .= '</td><td class="house">';
			$usersTable .= $user->house;
			$usersTable .= '</td><td class="edit">';
			$usersTable .= '<form  method="post" class="administer-event">
					<input type="hidden" name="' . self::$k_username . '" value="' . $user->username . '" />
					<input type="submit" name="' . self::$k_edit_user . '" value="Redigera!" />
					<input type="submit" name="' . self::$k_delete_user . '" class="' . self::$k_delete_user . '" value="Ta bort!" />
				</form>';
			$usersTable .= '</td></tr>';
		}
		$usersTable .= '</table></div>';
		
		return $usersTable;
	}

	public function triedToDeleteUser() {
		return isset($_POST[self::$k_delete_user]);
	}

	public function triedToEditUser() {
		return isset($_POST[self::$k_edit_user]);
	}

	public function getUserForUpdate() {
		$username = $this->getUserName();
		$email = $this->getEmail();
		$house = $this->getHouse();
		$role = $this->getRole();

		return User::createForUpdate($username, $email, $house, $role);
	}

	public function getUserForUpdateWithoutRole() {
		$email = $this->getUserName();

	}
	
	// Returnerar användarnamnet från $_POST
	public function getUserName() {
		if (isset($_POST[self::$k_username])) {
			$valRes = Validate::username($_POST[self::$k_username]);
			
			if ($valRes->isValid()) {
				return $valRes->getValue();
			}
			
			$this->validationErrors[] = 'Användarnamnet får endast bestå av bokstäver, siffror och understreck (_) samt vara mellan 3 och 30 tecken.';
			return null;
		}
		$this->validationErrors[] = 'Användarnamnet måste fyllas i.';
		return null;
	}
	
	public function getFullName() {
		if (isset($_POST[self::$k_fullName])) {
			
			$valRes = Validate::name($_POST[self::$k_fullName]);
			
			if ($valRes->isValid()) {
				return $valRes->getValue();
			}
			
			$this->validationErrors[] = 'Namnet måste vara mellan 3 och 30 tecken.';
			return null;
		}
		$this->validationErrors[] = 'Namnet måste fyllas i.';
		return NULL;
	}
	
	// Returnerar lösenordet från $_POST
	public function getPassword() {
		if (isset($_POST[self::$k_password]) == true && isset($_POST[self::$k_confirmPassword]) == true) {
			if ($_POST[self::$k_password] == $_POST[self::$k_confirmPassword]) {
				$valRes = Validate::password($_POST[self::$k_password]);
				
				if ($valRes->isValid()) {
					return md5($valRes->getValue());
				}
				
				$this->validationErrors[] = 'Lösenordet måste vara mellan 8 och 100 tecken.';
				return null;
			}
		}
		$this->validationErrors[] = 'Lösenord måste fyllas i två gånger.';
		return null;
	}
	
	// Returnerar email-adressen från $_POST
	public function getEmail() {
		if (isset($_POST[self::$k_email])) {
			$valRes = Validate::email($_POST[self::$k_email]);
			
			if ($valRes->isValid()) {
				return $valRes->getValue();
			}
			
			$this->validationErrors[] = 'En korrekt epostadress måste anges.';
			return null;
		}
		$this->validationErrors[] = 'Epost måste fyllas i.';
		return null;
	}
	
	public function getHouse() {
		if (isset($_POST[self::$k_house])) {			
			$valRes = Validate::email($_POST[self::$k_email]);

			return $_POST[self::$k_house];
		}
		$this->validationErrors[] = 'Förening måste fyllas i.';
		return null;
	}

	public function getRole() {
		if (isset($_POST[self::$k_role])) {
			return (int)array_search($_POST[self::$k_role], $this->roles);
		}
		return 0;
	}
	
	public function registrationIsValid() {
		return (count($this->validationErrors) == 0) ? true : false;
	}
	
	public function addUserExistsError() {
		$this->validationErrors[] = 'Användarnamnet är upptaget.';
	}
	
	public function addUserCreatedMsg($user) {
		$this->sucessMessages[] = 'Användaren ' . $user->username . ' har skapats.';
	}
	
	public function getSucessMsgs() {
		$html = '';
		if (isset($this->sucessMessages)) {
			$length = count($this->sucessMessages);
			if ($length > 0) {
				$html .= '<div class="success"><ul>';
				
				for ($i = 0; $i < $length; ++$i) {
					$html .= '<li>';
					$html .= $this->sucessMessages[$i];
					$html .= '</li>';
				}
				
				$html .= '</ul></div>';	
			}
		}
		return $html;		
	}
	
	public function getValidationErrors() {
		$html = '';
		if (isset($this->validationErrors)) {
			$length = count($this->validationErrors);
			if ($length > 0) {
				$html .= '<div class="errors"><ul>';
				
				for ($i = 0; $i < $length; ++$i) {
					$html .= '<li>';
					$html .= $this->validationErrors[$i];
					$html .= '</li>';
				}
				
				$html .= '</ul></div>';	
			}
		}
		return $html;		
	}
	
	

	// Indikerar ifall användaren vill registrera
	public function triedToRegister() {
		return isset($_POST[self::$k_register]);
	}
}
